package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.User;
import com.daka.entity.Welfare;
import com.daka.enumerate.DictionaryMap;
import com.daka.enumerate.StatusCode;
import com.daka.service.WelfareService;
import com.daka.tools.CheckLogin;
import com.daka.vo.request.WelfarePageRequestVo;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import com.daka.vo.response.PageListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/welfare")
@Slf4j
public class WelfareController {

    @Resource(name = "welfareService")
    private WelfareService welfareService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo list(HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.SYSTEM.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new ListDataResponseVo<User>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        List<Welfare> userList = welfareService.selectAll();
        return new ListDataResponseVo<Welfare>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), userList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/pageList", consumes = "application/json")
    @Login
    public DataResponseVo pageList(@Valid @RequestBody WelfarePageRequestVo welfarePageRequestVo, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.SYSTEM.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<PageListResponse<Welfare>>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        List<Welfare> welfareList = welfareService.selectByPage(welfarePageRequestVo.getCode(),welfarePageRequestVo.getStatus(), welfarePageRequestVo.getName(), welfarePageRequestVo.getPageIndex(), welfarePageRequestVo.getPageSize());
        int count = welfareService.selectCountByPage(welfarePageRequestVo.getCode(),welfarePageRequestVo.getStatus(), welfarePageRequestVo.getName(), welfarePageRequestVo.getPageIndex(), welfarePageRequestVo.getPageSize());

        PageListResponse<Welfare> page = new PageListResponse<Welfare>(welfareList, count);

        return new DataResponseVo<PageListResponse<Welfare>>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), page);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/save", consumes = "application/json")
    @Login
    public DataResponseVo save(@Valid @RequestBody Welfare welfare, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.SYSTEM.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Welfare>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        welfare = welfareService.save(welfare);

        return new DataResponseVo<Welfare>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), welfare);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete", consumes = "application/json")
    @Login
    public BaseResponseVo delete(@Valid @RequestBody Welfare welfare, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.SYSTEM.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Welfare>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        welfare = welfareService.selectById(welfare.getId());

        Welfare updateWelfare = new Welfare();
        updateWelfare.setId(welfare.getId());
        if (welfare.getDelStatus() == 0) {
            updateWelfare.setDelStatus(1);
        } else {
            updateWelfare.setDelStatus(0);
        }

        welfareService.updateById(updateWelfare);

        return new BaseResponseVo(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage());
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/push", consumes = "application/json")
    @Login
    public BaseResponseVo push(@Valid @RequestBody Welfare welfare, HttpSession session) {

        List<Integer> competenceList = new ArrayList<Integer>();
        competenceList.add(DictionaryMap.SYSTEM.getStatusCode());
        if (!CheckLogin.checkRole(session, competenceList)) {
            return new DataResponseVo<Welfare>(StatusCode.NO_COMPETENCE.getStatusCode(), StatusCode.NO_COMPETENCE.getStatusMessage());
        }

        welfare = welfareService.selectById(welfare.getId());

        Welfare updateWelfare = new Welfare();
        updateWelfare.setId(welfare.getId());
        if (welfare.getStatus() == 1) {
            updateWelfare.setStatus(2);
        } else {
            updateWelfare.setStatus(1);
        }

        welfareService.updateById(updateWelfare);

        return new BaseResponseVo(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage());
    }
}
